home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog6.arj / ED.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  4.5 KB  |  175 lines

  1. { ed.pas -- Edit controls demonstration }
  2.  
  3. program Ed;
  4.  
  5. {$R ed.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.  
  11.   id_Menu   = 100;  { Menu resource ID }
  12.   cm_Dialog = 101;  { Dialog-command ID }
  13.   id_Dialog = 200;  { Dialog resource ID }
  14.  
  15.   id_ed1    = 101;  { Single-line edit control ID }
  16.   id_ed2    = 102;  { Multiple-line edit control ID }
  17.   id_ed3    = 103;  { Password edit control ID }
  18.  
  19.   ed1Len    = 80;   { Maximum length of single-line control }
  20.   ed2Len    = 2048; { Maximum length of multiple-line control }
  21.   ed3Len    = 20;   { Maximum length of password control }
  22.  
  23. type
  24.  
  25.   PDialogRec = ^DialogRec;  { Pointer to DialogRec record }
  26.   DialogRec = record
  27.     SingleLine: array[0 .. ed1Len] of Char;
  28.     MultiLine: array[0 .. ed2Len] of Char;
  29.     Password: array[0 .. ed3Len] of Char;
  30.   end;
  31.  
  32.   PEdDialog = ^EdDialog;
  33.   EdDialog = object(TDialog)
  34.     DataPointer: PDialogRec;
  35.     constructor Init(AParent: PWindowsObject; AName: PChar;
  36.       P: PDialogRec);
  37.     procedure SetupWindow; virtual;
  38.     procedure Ok(var Msg: TMessage);
  39.       virtual id_First + id_Ok;
  40.   end;
  41.  
  42.   EdApplication = object(TApplication)
  43.     procedure InitMainWindow; virtual;
  44.   end;
  45.  
  46.   PEdWindow = ^EdWindow;
  47.   EdWindow = object(TWindow)
  48.     DialogData: DialogRec;
  49.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  50.     procedure CMDialog(var Msg: TMessage);
  51.       virtual cm_First + cm_Dialog;
  52.   end;
  53.  
  54.  
  55. { Common procedures }
  56.  
  57. {- Initialize edit control identified by CtrlID with text in Buffer.
  58. HDlg is the dialog window handle; MaxLen is the maximum number of
  59. characters that can be entered into the control.}
  60.  
  61. procedure SetText(HDlg: HWnd; CtrlID: Word; Buffer: PChar;
  62.   MaxLen: Word);
  63. begin
  64.   SendDlgItemMessage(HDlg, CtrlID, wm_SetText, 0, LongInt(Buffer));
  65.   SendDlgItemMessage(HDlg, CtrlID, em_LimitText, MaxLen, 0)
  66. end;
  67.  
  68. {- Retrieve text from edit control identified by CtrlID. Text is
  69. copied to the array addressed by Buffer. MaxLen equals the maximum
  70. number of bytes to copy, including the null terminator.}
  71.  
  72. procedure GetText(HDlg: HWnd; CtrlID: Word; Buffer: PChar;
  73.   MaxLen: Word);
  74. begin
  75.   SendDlgItemMessage(HDlg, CtrlID, wm_GetText, MaxLen,
  76.     LongInt(Buffer))
  77. end;
  78.  
  79.  
  80. { EdDialog }
  81.  
  82. {- Construct EdDialog instance. P addresses dialog data record. }
  83. constructor EdDialog.Init(AParent: PWindowsObject; AName: PChar;
  84.   P: PDialogRec);
  85. begin
  86.   TDialog.Init(AParent, AName);
  87.   DataPointer := P  { Save pointer to caller's dialog record }
  88. end;
  89.  
  90. {- Prepare initial control values }
  91. procedure EdDialog.SetupWindow;
  92. var
  93.   I: Integer;
  94. begin
  95.   TDialog.SetupWindow;
  96.   with DataPointer^ do
  97.   begin
  98.     SetText(HWindow, id_Ed1, SingleLine, ed1Len);
  99.     SetText(HWindow, id_Ed2, MultiLine, ed2Len);
  100.     SetText(HWindow, id_Ed3, Password, ed3Len)
  101.   end
  102. end;
  103.  
  104. {- Respond to Ok button }
  105. procedure EdDialog.Ok(var Msg: TMessage);
  106. var
  107.   TempPassword: array[0 .. ed3Len] of Char;
  108. begin
  109.   GetText(HWindow, id_Ed3, TempPassword, Sizeof(TempPassword));
  110.   if StrIComp(TempPassword, 'Password') <> 0 then
  111.   begin
  112.     MessageBeep(0);
  113.     MessageBox(HWindow,
  114.     'Incorrect password! (Enter "Password" in Password field)',
  115.     'Error', mb_Ok)
  116.   end else
  117.   begin
  118.     with DataPointer^ do
  119.     begin
  120.       GetText(HWindow, id_Ed1, SingleLine, Sizeof(SingleLine));
  121.       GetText(HWindow, id_Ed2, MultiLine, Sizeof(MultiLine));
  122.       GetText(HWindow, id_Ed3, Password, Sizeof(Password))
  123.     end;
  124.     TDialog.Ok(Msg)
  125.   end
  126. end;
  127.  
  128.  
  129. { EdApplication }
  130.  
  131. {- Initialize EdApplication object's window }
  132. procedure EdApplication.InitMainWindow;
  133. begin
  134.   MainWindow := New(PEdWindow, Init(nil, 'Ed'))
  135. end;
  136.  
  137.  
  138. { EdWindow }
  139.  
  140. {- Construct EdWindow object }
  141. constructor EdWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  142. begin
  143.   TWindow.Init(AParent, ATitle);
  144.   Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
  145.   with DialogData do
  146.   begin
  147.     StrCopy(SingleLine, 'Single-line string');
  148.     StrCopy(MultiLine, 'Multiple-line string');
  149.     StrCopy(Password, 'xxxxx')
  150.   end
  151. end;
  152.  
  153. {- Execute Menu:Dialog command }
  154. procedure EdWindow.CMDialog(var Msg: TMessage);
  155. begin
  156.   Application^.ExecDialog(New(PEdDialog,
  157.     Init(@Self, PChar(id_Dialog), @DialogData)))
  158. end;
  159.  
  160. var
  161.  
  162.   EdApp: EdApplication;
  163.  
  164. begin
  165.   EdApp.Init('EdApp');
  166.   EdApp.Run;
  167.   EdApp.Done
  168. end.
  169.  
  170.  
  171. {--------------------------------------------------------------
  172.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  173.   Revision 1.00    Date: 3/20/1991
  174. ---------------------------------------------------------------}
  175.